package servlet;

import Dao.LoginDao;
import com.alibaba.fastjson2.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        String username =  request.getParameter("username");
        String password =  request.getParameter("password");

        System.out.println(username+":" + password);

        JSONObject jsonObject = new JSONObject();

        if (LoginDao.checkUser(username,password)) {
            jsonObject.put("code",1);
        }else {
            jsonObject.put("code",0);;
        }

        PrintWriter pw = response.getWriter();
        pw.print(jsonObject);
        pw.close();

    }
}
